//
//  BRRentStateVC.m
//  Kexu
//
//  Created by IOS on 2023/7/22.
//

#import "BRRentStateVC.h"
#import "BROrderDetailViewController.h"

@interface BRRentStateVC ()
{
    QMUIButton *_btn;
}
@property (nonatomic, strong) dispatch_source_t timerSucc;

@end

@implementation BRRentStateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];

}

//定时器
-(void)startSuccTimeToBackHome {
    __weak __typeof__(self) weakSelf = self;
    __block int progressNum = 5; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    self.timerSucc = _timer;
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0), 1 * NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        if(progressNum <= 0){ //倒计时结束，关闭
            NSLog(@"订单结束");
            [weakSelf cancelTimer:_timer doMain:^{
                [weakSelf toOrderDetail];
            }];
        }else{
            progressNum--;
            dispatch_async(dispatch_get_main_queue(), ^{
               NSString *btnStr = [NSString stringWithFormat:@"%@(%d)",Localized(@"home_viewOrder"),progressNum];
                [self->_btn setTitle:btnStr forState:UIControlStateNormal];
                NSLog(@"请求次数--- %d --",progressNum);
            });
            
        }
    });
    
    dispatch_resume(_timer);
    
}

// 结束定时器并在异步主线程处理业务
- (void)cancelTimer:(dispatch_source_t)timer doMain:(void(^)(void))doMain {
    if (!timer) {
        doMain();
        return;
    }
    dispatch_source_cancel(timer);
    
    dispatch_async(dispatch_get_main_queue(), doMain);
}

- (void)toOrderDetail {
    [self cancelTimer:self.timerSucc doMain:^{
    }];
    BROrderDetailViewController *deVC = [[BROrderDetailViewController alloc] init];
    deVC.popType = 1;
    deVC.orderNo = self.orderNo;
    [self.navigationController pushViewController:deVC animated:YES];
}

- (void)pop {
    
    [self cancelTimer:self.timerSucc doMain:^{
    }];
    if (self.rentSucc) {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
       
//        [self toOrderDetail];
        return;
    }
    [self popToScanVC];
    
}

- (void)popToScanVC {
    
    [self.navigationController jk_popToViewControllerWithClassName:@"BRScanViewController" animated:YES];
}

- (BOOL)forceEnableInteractivePopGestureRecognizer {
    // 屏蔽右滑返回
    return NO;

}

#pragma mark - UI --
- (void)setUI {
    
    [self br_createTop:@""];
    
    UIImageView *stateImg = [[UIImageView alloc] init];
    [self.view addSubview:stateImg];
    [stateImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(141);
        make.top.offset(36+kSafeAreaTopHeight);
        make.centerX.offset(0);
    }];
    
    QMUILabel *titleL = [BRToolsView br_label:@"--" textColor:kColorTitle889 font:[UIFont fontH:20]];
    titleL.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(stateImg.mas_bottom).offset(0);
    }];
    
    QMUILabel *textL = [BRToolsView br_label:@"--" textColor:kColorTitle889 font:[UIFont fontM:14]];
    textL.textAlignment = NSTextAlignmentCenter;
    textL.numberOfLines = 0;
    [self.view addSubview:textL];
    [textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.right.offset(-28);
        make.top.equalTo(titleL.mas_bottom).offset(8);
    }];
    
    QMUIButton *btn = [BRToolsView br_btnBGImg:@"--"];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
        make.left.offset(14);
        make.right.offset(-14);
        make.bottom.offset(-(14 + kSafeAreaBottomHeight));
    }];
    _btn = btn;
    
    NSString *titleStr = Localized(@"home_failed");
    NSString *textStr = Localized(@"home_scanAgainText");
    NSString *btnStr = Localized(@"home_scan2rent");
    NSString *imgStr = @"scan_failed";
    UIColor *stateColor = UIColorMakeWithHex(@"#889DBB");
    if (self.rentSucc) {
        titleStr = Localized(@"home_success");
        textStr = Localized(@"home_takePBText");
        btnStr = [NSString stringWithFormat:@"%@(5)",Localized(@"home_viewOrder")];
        imgStr = @"scan_success";
        stateColor = UIColorMakeWithHex(@"#889DBB");
        [btn addTarget:self action:@selector(toOrderDetail) forControlEvents:UIControlEventTouchUpInside];
    }else {
        [btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    }
    
    stateImg.image = UIImageMake(imgStr);
    [btn setTitle:btnStr forState:UIControlStateNormal];
    titleL.text = titleStr;
    textL.text = textStr;
    titleL.textColor = stateColor;
    if (!self.rentSucc) return;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self startSuccTimeToBackHome];
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
